mui.init();


var old_back = mui.back;
mui.back = function(){
	//显示上一个界面；
var page=mui.currentWebview.opener();

page.show();
mui.fire(page,'reload',{});
//继续当前页面原有返回逻辑
old_back();
}

/**
 * 当从登录页面返回时，回调用当前“刷新”事件 
 */
window.addEventListener('reload',function(event){
	reload();
});

function reload(){
	resetForm();
	loadData();
}


function resetForm(){
	
	FormMgr.resetFields(["money","jpwd","phone","checkbox"],true); /*清空密码字段*/
	var url = UrlConfig.mwithdraw_getInfo;
	var kyye;
	MuiEventMgr.ajax(url,{
	    pars:{},
	  	sfn : function(data){
	  		data = data.msg;
	  		kyye = data.kyye;
	  		$("#sp_kyye").text("￥" + kyye);
	  		},ffn : function(){
	 		mui.alert("获取提现信息失败!");
	 		}
	}); 		
	$("#bankCardBox").hide();
	$("#div_hongbao").hide();
	$("#sp_name").html("暂无");
	$("#sp_bankNumber").html("");
	$("#sp_bankName").html("暂无");
	$("#lb_hongbao").text("￥0");
	$("#sendsms").html("获取短信");
	stopTimer();
}

function loadData(){
	var url = UrlConfig.mwithdraw_getInfo;
	MuiEventMgr.ajax(url,{
	  	pars:{},
	  	sfn : function(data){
	  		var success = data.success;
			if(!success){
				var status = data.status;
				var msg = data.msg;
				var _msg = "提现之前请先绑定银行卡，确定现在绑定银行卡";//msg.msg;
				var status = msg.status;
				var btn = ["确定","取消"];
				mui.confirm(_msg,'提示',btn,function(e){
					if(e.index==0){
						forward_bankPage(status);
					}
				});
			}else{
				renderDatas(data);
			}
	 	},ffn : function(){
	 		mui.alert("获取提现信息失败!");
	 	}
	});
}


var cls_appId = null;
var widthdraw_count = 0;
function renderDatas(data){
 	data = data.msg;
	var appId = data.appId;
	var bankName = data.bankName;
	var bankAccount = data.bankAccount;
	var name = data.name;
	var phone = data.phone;
	var count = data.count;
	widthdraw_count = count;
	var hongbao = data.hongbao || 0;
	var kyye = data.kyye;
	$("#sp_name").text(name);
	var clssPrefix = "banksmall-";
	if(cls_appId) $("#sp_bankIcon").removeClass(clssPrefix+cls_appId);
	if(!bankName){
		$("#bankCardBox").hide();
	}else{
		$("#bankCardBox").show();
		$("#sp_bankIcon").addClass(clssPrefix+appId);
		$("#sp_bankName").text(bankName);
		$("#sp_bankNumber").html("(&nbsp;&nbsp;"+bankAccount+"&nbsp;&nbsp;)");
	}
	$("#sp_kyye").text("￥" + kyye);
	if(hongbao && parseFloat(hongbao) > 0){
		$("#div_hongbao").show();
	}
	$("#lb_hongbao").text(hongbao);
	cls_appId = appId;
}


function forward_bankPage(){
	var url = "../mybank/mybank.html";
	var targetPageId = plus.webview.currentWebview().id;
	plus.storage.setItem(KeyCfg.targetPageId_anquanyz,targetPageId);
	var cfg = {pars:{}};
	MuiEventMgr.checkSecuityForwad(function(){
		openPage(url,{});
		console.log("targetPageId="+plus.storage.getItem(KeyCfg.targetPageId));
	});
			
}

/**
 * 打开新页面 
 * @param {Object} url	新地址
 * @param {Object} pars	参数
 */
function openPage(url ,pars){
	if(!pars) pars = {};
	var cfg = {pars : pars};
	MuiEventMgr.openBySafe(url,cfg);
}

mui.plusReady(function() {
	(function(){
		resetForm();
		loadData();
	})();
	
	(function(){/*初始化事件*/
		var a_changeCard = CmwMcore.$('a_changeCard');
		MeventMgr.onTouchstart(a_changeCard,function(event,self){
			forward_bankPage();
		});
		
		var btnSendsms = CmwMcore.$('sendsms');
		MeventMgr.onTouchstart(btnSendsms,function(event,self){
			 var text = $("#sendsms").text();
			 if(text != "获取短信" && text != "重新获取") return;
			 $("#phone").val("");
			 var userId = plus.storage.getItem(KeyCfg.userId);
			 var pars = {userId:userId,smstype:"validCash"};
			 sendSms(pars);
		});
		
		//申请提现事件
		var btn_login = CmwMcore.$('btn_login');
		MeventMgr.onTouchstart(btn_login,function(event,self){
			var dis=$("#btn_login").attr("disabled")//判断是否有这属性
			if(dis)return;
			if(!valid()) return false;
			var money = $("#money").val();
			var url = UrlConfig.mwithdraw_ajaxValidate;
			MuiEventMgr.ajax(url,{
			  	pars:{param:money},
			  	sfn : function(data){
			  		var success = data.success; 
			  		var msg = data.msg;
			  		if(success){
			  			submitForm();
			  		}else{
			  			mui.alert(msg);
			  		}
			  	},ffn : function(data){
			  		mui.alert("检查提现金额发生错误");
			  	}
			 });
		});
	})();
 
	
	
	
	/*表单验证*/
	function valid(){
		var eleMoney = $('#money');
		var money_val = eleMoney.val();
		if(!money_val || parseFloat(money_val) <= 0){
			eleMoney.focus();
			mui.alert("请输入大于0的提现金额!");
			return false;
		}
		
		var elePwd = $('#jpwd');
		var pwd_val = elePwd.val();
		if(!pwd_val){
			elePwd.focus();
			mui.alert("请输入交易密码!");
			return false;
		}
		
		var elePhone = $('#phone');
		var phone_val = elePhone.val();
		if(!phone_val){
			elePhone.focus();
			mui.alert("请输入短信验证码!");
			return false;
		}
		return true;
	}
	
	function submitForm(){
		CmwMcore.disabled('btn_login');
		var formDatas = $("#theForm").serialize();
		var url = UrlConfig.mwithdraw_subWith;
		
		MuiEventMgr.ajax(url,{
		  	pars:formDatas,
		  	sfn : function(data){
		  		callback(data);
	        	CmwMcore.enabled('btn_login');
		  	},ffn : function(data){
		  		CmwMcore.enabled('btn_login');
		  	}
		 });
	}
	
	function callback(data){
		var success = data.success;
		var msg = data.msg;
		CmwMcore.enabled('btn_login');
		if(success){
			mui.alert("申请提现成功,请等待审核","成功");
			reload();
			return;
		}else{
		   if(msg=='false'){
               mui.alert("申请提现失败！", "友情提示");
           }else if(msg=='noCard'){
				mui.alert("没有绑定银行卡！", "友情提示");
           }else if(msg=='phone'){
               mui.alert("手机验证码错误！", "友情提示");
           }else if(msg=='myz'){
               mui.alert("请先发送验证码！", "友情提示");
           }else if(msg=='yzsx'){
               mui.alert("验证码失效！", "友情提示");
           }else if(msg=='ce1'){
           		mui.alert("您最多只能提取300000.00！", "友情提示");
           }else if(msg=='ce'){
           		mui.alert("您最多只能提取500000.00！", "友情提示");
           }else if(msg=='yebz'){
           		mui.alert("您提现的余额不足！", "友情提示");
           }else if(msg=='jy'){
           		mui.alert("交易密码有误！", "友情提示");
           }else if(msg="count"){
           		mui.alert("当天提现次数不能超过"+widthdraw_count+"次！", "友情提示");
           }else if(msg=='lock') {
           		mui.alert("交易密码连续5次错误，为保护您帐户安全，将暂时锁定，请24小时后重试！", "友情提示");
           }else if(msg=='busy'){
           	mui.alert("系统繁忙，请稍后再试！", "友情提示");
           }
		}
	}
	
});